package com.simple.proxy.mybatis.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TransactionManager {
    public static Map<Thread, Connection> threadConnectionData = new ConcurrentHashMap<>();

    public static Connection getConnection(){
        Connection connection = threadConnectionData.get(Thread.currentThread());
        try {
            //判断当前线程有没有数据库连接，没有则重新创建
            if(connection == null){
                connection = DbTools.getConnection();
                threadConnectionData.put(Thread.currentThread(),connection);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }
}
